import 'package:get/get.dart';

import './base_repository.dart';
import '../providers/device_provider.dart';
import '../models/device_model.dart';

/// 设备仓库
/// 负责封装设备数据获取和控制的逻辑，可能包含缓存策略
class DeviceRepository extends BaseRepository {
  // 注入设备提供者
  final DeviceProvider _deviceProvider = Get.find<DeviceProvider>();

  /// 获取设备列表
  Future<List<DeviceModel>> getDeviceList({
    String? userId,
    int page = 1,
    int limit = 20,
    Map<String, dynamic>? filters,
  }) async {
    // 可以在这里实现缓存逻辑，例如：
    // final cacheKey = 'device_list_${userId}_${page}_${limit}_${filters?.hashCode}';
    // return fetchWithCache(
    //   cacheKey,
    //   () async {
    //     final response = await _deviceProvider.getDeviceList(userId: userId, page: page, limit: limit, filters: filters);
    //     if (response.isSuccess && response.data != null) return response.data;
    //     return null;
    //   },
    //   (cachedData) => (cachedData as List).map((item) => DeviceModel.fromJson(item)).toList(),
    //   (data) => data.map((item) => item.toJson()).toList(),
    // );
    
    // 无缓存逻辑：直接调用 Provider
    final response = await _deviceProvider.getDeviceList(
      userId: userId, page: page, limit: limit, filters: filters
    );
    if (response.isSuccess && response.data != null) {
      return response.data!;
    } else {
      if (!response.isSuccess) {
        throw Exception('获取设备列表失败: ${response.message}');
      }
      return []; // 成功但无数据
    }
  }

  /// 获取设备详情
  Future<DeviceModel?> getDeviceById(String deviceId) async {
    // 可以添加缓存逻辑
    final response = await _deviceProvider.getDeviceById(deviceId);
    if (response.isSuccess && response.data != null) {
      return response.data!;
    } else {
       if (!response.isSuccess) {
        throw Exception('获取设备 $deviceId 详情失败: ${response.message}');
      }
      return null; // 成功但无数据 (理论上不应该发生)
    }
  }

  /// 发送设备命令
  Future<bool> sendDeviceCommand(
    String deviceId,
    String command,
    Map<String, dynamic>? params,
  ) async {
    final response = await _deviceProvider.sendDeviceCommand(deviceId, command, params);
    // 检查特定响应码或字段来判断是否成功
    return response.isSuccess; // 简单判断，可根据后端响应调整
  }

  /// 添加设备
  Future<DeviceModel?> addDevice(Map<String, dynamic> deviceData) async {
    final response = await _deviceProvider.addDevice(deviceData);
    if (response.isSuccess && response.data != null) {
      return response.data!;
    } else {
      throw Exception('添加设备失败: ${response.message}');
    }
  }

  /// 更新设备信息
  Future<DeviceModel?> updateDevice(String deviceId, Map<String, dynamic> updateData) async {
    final response = await _deviceProvider.updateDevice(deviceId, updateData);
    if (response.isSuccess && response.data != null) {
      return response.data!;
    } else {
      throw Exception('更新设备 $deviceId 失败: ${response.message}');
    }
  }

  /// 删除设备
  Future<bool> deleteDevice(String deviceId) async {
    final response = await _deviceProvider.deleteDevice(deviceId);
    return response.isSuccess;
  }
} 